package com.supermap.dataservice.cloud.warnInfo;

import com.supermap.dubbo.api.cloud.warnInfo.WarnInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年12月03 下午 4:42
 * @email 1301584033@qq.com
 */
@Mapper
public interface WarningInfoMapper {
    /**
     *多条入库
     * */
    @InsertProvider(type = WarningInfoMapperSQL.class,method = "insertWarnInfoListSQL")
    int insertWarnInfoList(@Param("warnInfoList")List<WarnInfo> warnInfoList );
    /**
     *根据预警ID查询出一条信息
     * */
    @Select("select * from t_warning_info where id=#{warnInfoId}")
    WarnInfo getWarnInfoById(@Param("warnInfoId") Integer warnInfoId);

    @SelectProvider(type = WarningInfoMapperSQL.class,method = "getPageWarnInfoByTimeOrAreaCodeSQL")
    List<WarnInfo>  getPageWarnInfoByTimeOrAreaCode(@Param("areaCode") String areaCode, @Param("beginTime")String beginTime, @Param("endTime")String endTime, @Param("pageNum")int pageNum, @Param("pageSize")int pageSize);

    @SelectProvider(type = WarningInfoMapperSQL.class,method = "getPageWarnInfoByTimeOrAreaCodeCountNumSQL")
    Integer  getPageWarnInfoByTimeOrAreaCodeCountNum(@Param("areaCode") String areaCode, @Param("beginTime")String beginTime, @Param("endTime")String endTime, @Param("pageNum")int pageNum, @Param("pageSize")int pageSize);

    class WarningInfoMapperSQL{
        public String getPageWarnInfoByTimeOrAreaCodeSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String areaCode=(String)map.get("areaCode");
            String beginTime=(String)map.get("beginTime");
            String endTime=(String)map.get("endTime");
            int pageNum=(int)map.get("pageNum");
            int pageSize=(int)map.get("pageSize");
            stringBuilder.append("select * from t_warning_info where 1=1 ");
            if(areaCode!=null){
                stringBuilder.append(" and   areaCode like concat("+areaCode+",'%') ");
            }
            if(beginTime!=null&&endTime!=null){
                stringBuilder.append("  and expires between '"+beginTime+"' and '"+endTime+" 23:59:59' ");
            }
            stringBuilder.append("order by expires desc limit "+(pageNum-1)*pageSize  +","+pageSize+"");
            return  stringBuilder.toString();
        }
        public String getPageWarnInfoByTimeOrAreaCodeCountNumSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String areaCode=(String)map.get("areaCode");
            String beginTime=(String)map.get("beginTime");
            String endTime=(String)map.get("endTime");
            int pageNum=(int)map.get("pageNum");
            int pageSize=(int)map.get("pageSize");
            stringBuilder.append("select count(1) from t_warning_info where 1=1 ");
            if(areaCode!=null){
                stringBuilder.append(" and  areaCode like concat("+areaCode+",'%') ");
            }
            if(beginTime!=null&&endTime!=null){
                stringBuilder.append("  and expires between '"+beginTime+"' and '"+endTime+"'");
            }
            return  stringBuilder.toString();
        }


        /**
         *多条入库
         * */
        public String insertWarnInfoListSQL(Map<String,Object> map){
            List<WarnInfo> warnInfoList=(List<WarnInfo>)map.get("warnInfoList");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" insert  IGNORE  into t_warning_info(id,severity,sender,referencesInfo,note,msgType,identifier,headline,expires,eventTypeCN,eventType,effective,description,areaCode) ");
            stringBuilder.append(" values ");
            if(warnInfoList!=null&&warnInfoList.size()>0){
                for (WarnInfo warnInfo:warnInfoList){
                    stringBuilder.append("(");
                    stringBuilder.append("'"+warnInfo.getId()+"',");
                    stringBuilder.append("'"+warnInfo.getSeverity()+"',");
                    stringBuilder.append("'"+warnInfo.getSender()+"',");
                    stringBuilder.append("'"+warnInfo.getReferencesInfo()+"',");
                    stringBuilder.append("'"+warnInfo.getNote()+"',");
                    stringBuilder.append("'"+warnInfo.getMsgType()+"',");
                    stringBuilder.append("'"+warnInfo.getIdentifier()+"',");
                    stringBuilder.append("'"+warnInfo.getHeadline()+"',");
                    stringBuilder.append("'"+warnInfo.getExpires()+"',");
                    stringBuilder.append("'"+warnInfo.getEventTypeCN()+"',");
                    stringBuilder.append("'"+warnInfo.getEventType()+"',");
                    stringBuilder.append("'"+warnInfo.getEffective()+"',");
                    stringBuilder.append("'"+warnInfo.getDescription()+"',");
                    stringBuilder.append("'"+warnInfo.getAreaCode()+"'");
                    stringBuilder.append("),");
                }
            }
            return stringBuilder.substring(0,stringBuilder.lastIndexOf(",")).toString();

        }
    }
}
